//set working directory 
cd  "C:\Users\johna\Dropbox (Curiel Analytx)\ElectionNightResults\replication_code"
//the above is just the path to the replcation files. The data from here on are 
//hierarchical and nested from this file path. 
** Get poll closing times


//read in the state dta file 
use "data\nyt_states.dta", clear


append using "data\scratch\hour60"
sort state hoursfromclose

//recalc a field 
gen total2 = dem + rep
//gen norm_total = 100*(dem + rep)/leip_tvotes
//gen norm_dempct = dempct - leip_dempct

** drop two dummy rows that had been added before
drop if hoursfromclose == 60
drop if hoursfromclose > 60.00999 & hoursfromclose < 60.01 // 60.01 isn't precisely represented
drop if hoursfromclose == 6

//drop maxtotal field from earlier data 
drop maxtotal

** calculate reporting momentum

gen total4 = norm_total if hoursfromclose <= 4 & hoursfromclose[_n+1] > 4
gen total8 = norm_total if hoursfromclose <= 8 & hoursfromclose[_n+1] > 8
gen total24 = norm_total if hoursfromclose <= 24 & hoursfromclose[_n+1] > 24
gen total48 = norm_total if hoursfromclose <= 48 & hoursfromclose[_n+1] > 48

egen maxtotal4 = max(total4),by(state)
egen maxtotal8 = max(total8),by(state)
egen maxtotal24 = max(total24),by(state)
egen maxtotal48 = max(total48),by(state)



keep if state ~= state[_n+1]
replace maxtotal4 = 94.41113 if state == "hawaii"


gsort -maxtotal4
gen order = _n
egen maxrange = rowmax(maxtotal*)
egen minrange = rowmin(maxtotal*)
//gen zero = 0

gen state_lbl = proper(subinstr(state,"-"," ",.))
replace state_lbl = "D.C." if state_lbl == "District Of Columbia"
//


twoway (pcspike order minrange order maxrange, lcolor(grey) lwidth(thick)) ///
	   (scatter order maxtotal4, mcolor(blue) msize(small) msymbol(circle)) ///
	   (scatter order maxtotal8, mcolor(midblue) msize(small) msymbol(circle)) ///
	   (scatter order maxtotal24, mcolor(teal) msize(small) msymbol(circle)) ///
	   (scatter order maxtotal48, mcolor(navy) msize(small) msymbol(circle)) ///
	   (scatter order zero, msymbol(none) mlabel(state_lbl) mlabsize(vsmall) mlabposition(9)) ///
	   , ///
	   ytitle("") ylabel(1(1)51, nolabels) ///
	   xtitle(Percent of final vote reported) xlabel(0 10 20 30 40 50 60 70 80 90 100 "100%",labsize(small)) xmtick(0(2)100, grid) ///
	   xline(100) ///
	   legend(order(2 "4 hours" 3 "8 hours" 4 "24 hours" 5 "48 hours") title(Hours after polls closed)) xsize(4) ysize(6) aspectratio(2)
graph export "figures\fig3.png", replace width(2500)
